package com.smartStatus.core.util;


import lombok.extern.slf4j.Slf4j;

import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import java.security.KeyFactory;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName : SecretUtils
 * @Author : lby
 * @Date: 2020/12/7 15:59
 * @Description : 密码加密解密类
 */
@Slf4j
public class SecretUtils {

    private static final String PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCrLGSRIZ+q/w4URaESS1hPGHDDCxp/8acDBvriLzCwh/ba0ARPsHRX4yIZ2hCr/LCa35HcWlfiowDSei+u1OYaTE1zOjFVjyHMk/G41YqHa1oCHamoX1/9Vv6JVuoh8iE7MtxuG8BS3eI6L/ikNlMle/Wmddrn8R9quxVjMTsiXQIDAQAB";

    private static final String PRIVATE_KEY = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAKssZJEhn6r/DhRFoRJLWE8YcMMLGn/xpwMG+uIvMLCH9trQBE+wdFfjIhnaEKv8sJrfkdxaV+KjANJ6L67U5hpMTXM6MVWPIcyT8bjViodrWgIdqahfX/1W/olW6iHyITsy3G4bwFLd4jov+KQ2UyV79aZ12ufxH2q7FWMxOyJdAgMBAAECgYEAmtNmi1dMYpCcnX11eWSpFbHXnLOYXo2nMLfJiXiBo8r3X2ez3piOQ0hwec2iMs42t37tIdCdmaMyhS57nrl2/mr3YChHeLpw+lTJjavZ4z0A2eKqvcro/JlEC56ItDD925LMsVjFq2w5x56Uq3HyPF9IQGYlKkyrNiZL4UuqL0kCQQDfTHXXMgb2lg7JK73ocJVwEyU1wfs4I/4CgCnhsYkjmMeOX76+oRS/wFsLIqrzNQHHs7ySVOviDgecvQBJoBjDAkEAxD29Tu0GtVFgQpcNSqUWQRU0E0Ue+gg8gblnoFHU9UDw+9WoKZ9MRk7E0xdrDAE0B9ik26Xn8YExbjJZffcmXwJAH/hLetbvxkgvr7+yfd/4lUY4cpzmQeBtUqBU3rpTXyRVld3BanuXg+ifYFXDUfTJ33StT+oWp5PIpTkuHFxujwJAfOywrqXFN3fuR3XRSEt/tz7AP+QorBLCMfLNPQna/6WvwyFoQ9bcv4U1Vwv5YVMCxFvt1sXuuZb82KHExqOnEQJBANCbkInbiZzJKUNQe8pssJm65jNKHii0r9+RdCHRGDC0mYTFDp/r1dBGGXTMnKTxsa113xbHazKi5MMKLqnTXtw=";
    private static Map<Integer, String> keyMap = new HashMap<Integer, String>();

    /**
     * RSA公钥加密
     *
     * @param str 加密字符串
     * @param 公钥
     * @return 密文
     * @throws Exception 加密过程中的异常信息
     */
    public static String encrypt(String str) throws Exception {
        //base64编码的公钥
        byte[] decoded = Base64.decodeBase64(PUBLIC_KEY);
        RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded));
        //RSA加密
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, pubKey);
        String outStr = Base64.encodeBase64String(cipher.doFinal(str.getBytes("UTF-8")));
        return outStr;
    }

    /**
     * RSA私钥解密
     *
     * @param str 加密字符串
     * @param 私钥
     * @return 铭文
     * @throws Exception 解密过程中的异常信息
     */
    public static String decrypt(String str) throws Exception {
        //64位解码加密后的字符串
        byte[] inputByte = Base64.decodeBase64(str.getBytes("UTF-8"));
        //base64编码的私钥
        byte[] decoded = Base64.decodeBase64(PRIVATE_KEY);
        RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(decoded));
        //RSA解密
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, priKey);
        String outStr = new String(cipher.doFinal(inputByte));
        return outStr;
    }

}

